package com.csot.ry.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csot.model.CheckSegment;
import com.csot.ry.domain.TCheckSegment;
import com.csot.ry.mapper.TCheckSegmentMapper;
import com.csot.ry.service.ITCheckSegmentService;
import com.csot.ry.util.CacheKitUtil;
import com.csot.common.utils.DateUtils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.net.NetUtil;

/**
 * 数据稽核Service业务层处理
 * 
 * @author 刘沙
 * @date 2022-06-02
 */
@Service
public class TCheckSegmentServiceImpl implements ITCheckSegmentService {
	@Autowired
	private TCheckSegmentMapper tCheckSegmentMapper;

	private CheckSegment dao = new CheckSegment().dao();

	/**
	 * 查询数据稽核
	 * 
	 * @param id 数据稽核主键
	 * @return 数据稽核
	 */
	@Override
	public TCheckSegment selectTCheckSegmentById(Long id) {
		return tCheckSegmentMapper.selectTCheckSegmentById(id);
	}

	/**
	 * 查询数据稽核列表
	 * 
	 * @param tCheckSegment 数据稽核
	 * @return 数据稽核
	 */
	@Override
	public List<TCheckSegment> selectTCheckSegmentList(TCheckSegment tCheckSegment) {
		return tCheckSegmentMapper.selectTCheckSegmentList(tCheckSegment);
	}

	/**
	 * 新增数据稽核
	 * 
	 * @param tCheckSegment 数据稽核
	 * @return 结果
	 */
	@Override
	public int insertTCheckSegment(TCheckSegment tCheckSegment) {
		tCheckSegment.setCreateTime(DateUtils.getNowDate());
		return tCheckSegmentMapper.insertTCheckSegment(tCheckSegment);
	}

	/**
	 * 修改数据稽核
	 * 
	 * @param tCheckSegment 数据稽核
	 * @return 结果
	 */
	@Override
	public int updateTCheckSegment(TCheckSegment tCheckSegment) {
		tCheckSegment.setUpdateTime(DateUtils.getNowDate());
		return tCheckSegmentMapper.updateTCheckSegment(tCheckSegment);
	}

	/**
	 * 批量删除数据稽核
	 * 
	 * @param ids 需要删除的数据稽核主键
	 * @return 结果
	 */
	@Override
	public int deleteTCheckSegmentByIds(Long[] ids) {
		return tCheckSegmentMapper.deleteTCheckSegmentByIds(ids);
	}

	/**
	 * 删除数据稽核信息
	 * 
	 * @param id 数据稽核主键
	 * @return 结果
	 */
	@Override
	public int deleteTCheckSegmentById(Long id) {
		return tCheckSegmentMapper.deleteTCheckSegmentById(id);
	}

	@Override
	public List<TCheckSegment> importCheckSegment(List<TCheckSegment> tCheckSegmentList) {
		// TODO Auto-generated method stub
		List<TCheckSegment> result = new ArrayList<>();

		for (TCheckSegment tcs : tCheckSegmentList) {

			String city_e = CacheKitUtil.ip_city.getKey(tcs.getIpCity());
			String ip_type = CacheKitUtil.ip_type.getKey(tcs.getIpType());
			Long ip_start_i = NetUtil.ipv4ToLong(tcs.getIpStart().trim());
			Long ip_end_i = NetUtil.ipv4ToLong(tcs.getIpEnd().trim());

			List<CheckSegment> csList = dao.find(
					"select * from t_city_segment where ip_start_i <= ? and ip_end_i >= ? and ip_type = ? and ip_city = ? order by ip_start_i",
					ip_end_i, ip_start_i, ip_type, city_e);

			if (!CollUtil.isEmpty(csList)) {
				if (ip_start_i < csList.get(0).getIpStartI()) {
					long end_i = csList.get(0).getIpStartI();
					TCheckSegment t = new TCheckSegment();
					BeanUtils.copyProperties(tcs, t);
					t.setIpStartI(ip_start_i);
					t.setIpEndI(--end_i);
					t.setIpStart(tcs.getIpStart());
					t.setIpEnd(NetUtil.longToIpv4(end_i));
					result.add(t);
				} else {
					csList.get(0).setIpStartI(ip_start_i);
					csList.get(0).setIpStart(tcs.getIpStart());
				}

				if (ip_end_i > csList.get(csList.size() - 1).getIpEndI()) {
					TCheckSegment t = new TCheckSegment();
					long start_i = csList.get(csList.size() - 1).getIpEndI();
					BeanUtils.copyProperties(tcs, t);
					t.setIpStartI(++start_i);
					t.setIpEndI(ip_end_i);
					t.setIpStart(NetUtil.longToIpv4(start_i));
					t.setIpEnd(tcs.getIpEnd());
					result.add(t);
				} else {
					csList.get(csList.size() - 1).setIpEndI(ip_end_i);
					csList.get(csList.size() - 1).setIpEnd(tcs.getIpEnd());
				}
			} else {
				TCheckSegment t = new TCheckSegment();
				BeanUtils.copyProperties(tcs, t);
				t.setIpStartI(ip_start_i);
				t.setIpEndI(ip_end_i);
				t.setIpStart(tcs.getIpStart());
				t.setIpEnd(tcs.getIpEnd());
				result.add(t);
			}

			for (int i = 0, j = i + 1; i < csList.size() && j < csList.size(); i++, j++) {
				long end_i = csList.get(j).getIpStartI();
				long start_i = csList.get(i).getIpEndI();
				System.out.println("-----------111---------:" + end_i);
				if ((++start_i) > (--end_i)) {
					continue;
				}
				System.out.println("-----------222---------:" + end_i);

				TCheckSegment t = new TCheckSegment();
				BeanUtils.copyProperties(tcs, t);
				t.setIpStartI(start_i);
				t.setIpStart(NetUtil.longToIpv4(start_i));
				t.setIpEnd(NetUtil.longToIpv4(end_i));
				t.setIpEndI(end_i);
				result.add(t);
			}

		}

		return result;
	}

	@Override
	public List<TCheckSegment> importCheckSegmentCoarse(List<TCheckSegment> tCheckSegmentList) {
		// TODO Auto-generated method stub
		List<TCheckSegment> result = new ArrayList<>();

		for (TCheckSegment tcs : tCheckSegmentList) {

			String city_e = CacheKitUtil.ip_city.getKey(tcs.getIpCity());
			String ip_type = CacheKitUtil.ip_type.getKey(tcs.getIpType());
			Long ip_start_i = NetUtil.ipv4ToLong(tcs.getIpStart().trim());
			Long ip_end_i = NetUtil.ipv4ToLong(tcs.getIpEnd().trim());

			List<CheckSegment> csList = dao.find(
					"select * from t_coarse_segment where ip_start_i <= ? and ip_end_i >= ? and ip_type = ? and ip_city = ? order by ip_start_i",
					ip_end_i, ip_start_i, ip_type, city_e);

			if (!CollUtil.isEmpty(csList)) {
				if (ip_start_i < csList.get(0).getIpStartI()) {
					long end_i = csList.get(0).getIpStartI();
					TCheckSegment t = new TCheckSegment();
					BeanUtils.copyProperties(tcs, t);
					t.setIpStartI(ip_start_i);
					t.setIpEndI(--end_i);
					t.setIpStart(tcs.getIpStart());
					t.setIpEnd(NetUtil.longToIpv4(end_i));
					result.add(t);
				} else {
					csList.get(0).setIpStartI(ip_start_i);
					csList.get(0).setIpStart(tcs.getIpStart());
				}

				if (ip_end_i > csList.get(csList.size() - 1).getIpEndI()) {
					TCheckSegment t = new TCheckSegment();
					long start_i = csList.get(csList.size() - 1).getIpEndI();
					BeanUtils.copyProperties(tcs, t);
					t.setIpStartI(++start_i);
					t.setIpEndI(ip_end_i);
					t.setIpStart(NetUtil.longToIpv4(start_i));
					t.setIpEnd(tcs.getIpEnd());
					result.add(t);
				} else {
					csList.get(csList.size() - 1).setIpEndI(ip_end_i);
					csList.get(csList.size() - 1).setIpEnd(tcs.getIpEnd());
				}
			} else {
				TCheckSegment t = new TCheckSegment();
				BeanUtils.copyProperties(tcs, t);
				t.setIpStartI(ip_start_i);
				t.setIpEndI(ip_end_i);
				t.setIpStart(tcs.getIpStart());
				t.setIpEnd(tcs.getIpEnd());
				result.add(t);
			}

			for (int i = 0, j = i + 1; i < csList.size() && j < csList.size(); i++, j++) {
				long end_i = csList.get(j).getIpStartI();
				long start_i = csList.get(i).getIpEndI();
				System.out.println("-----------111---------:" + end_i);
				if ((++start_i) > (--end_i)) {
					continue;
				}
				System.out.println("-----------222---------:" + end_i);

				TCheckSegment t = new TCheckSegment();
				BeanUtils.copyProperties(tcs, t);
				t.setIpStartI(start_i);
				t.setIpStart(NetUtil.longToIpv4(start_i));
				t.setIpEnd(NetUtil.longToIpv4(end_i));
				t.setIpEndI(end_i);
				result.add(t);
			}

		}

		return result;
	}

}
